﻿
(function ($) {
    //搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
    $.fn.zTree.search = function (treeId, nameKey, searchCondition, flag) {
        /// <summary>
        /// 搜索树，高亮显示并展示【模糊匹配搜索条件的节点s】
        /// </summary>
        /// <param name="treeId">ztree的Id</param>
        /// <param name="nameKey">显示字段名</param>
        /// <param name="searchCondition">搜索匹配字符串</param>
        /// <param name="flag">需要高亮显示的节点标识</param>

        //<1>.得到模糊匹配搜索条件的节点数组集合
        var highlightNodes = new Array();
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        if (searchCondition != "") {
            highlightNodes = treeObj.getNodesByParamFuzzy(nameKey, searchCondition, null);
        }
        //<2>.高亮显示并展示【指定节点s】
        highlightAndExpand_ztree(treeId, highlightNodes, flag);
    }

    /**
     * 高亮显示并展示【指定节点s】
     * @param treeId
     * @param highlightNodes 需要高亮显示的节点数组
     * @param flag           需要高亮显示的节点标识
     */
    function highlightAndExpand_ztree(treeId, highlightNodes, flag) {
        var treeObj = $.fn.zTree.getZTreeObj(treeId);
        //<1>. 先把全部节点更新为普通样式
        var treeNodes = treeObj.transformToArray(treeObj.getNodes());
        for (var i = 0; i < treeNodes.length; i++) {
            treeNodes[i].highlight = false;
            treeObj.updateNode(treeNodes[i]);
        }
        //<2>.收起树   先取消动画，避免收不全的bug，然后再恢复动画设置
        var expandSpeed = treeObj.setting.view.expandSpeed;
        treeObj.setting.view.expandSpeed = "";
        treeObj.expandAll(false);
        treeObj.setting.view.expandSpeed = expandSpeed;
        //<3>.把指定节点的样式更新为高亮显示，并展开
        if (highlightNodes != null) {
            for (var i = 0; i < highlightNodes.length; i++) {
                if (flag != null && flag != "") {
                    if (highlightNodes[i].flag == flag) {
                        //高亮显示节点，并展开
                        highlightNodes[i].highlight = true;
                        treeObj.updateNode(highlightNodes[i]);
                        //高亮显示节点的父节点的父节点....直到根节点，并展示
                        var parentNode = highlightNodes[i].getParentNode();
                        treeObj.expandNode(parentNode, true, false, true);
                        var parentNodes = getParentNodes_ztree(treeId, parentNode);
                        treeObj.expandNode(parentNodes, true, false, true);
                    }
                } else {
                    //高亮显示节点，并展开
                    highlightNodes[i].highlight = true;
                    treeObj.updateNode(highlightNodes[i]);
                    //高亮显示节点的父节点的父节点....直到根节点，并展示
                    var parentNode = highlightNodes[i].getParentNode();
                    treeObj.expandNode(parentNode, true, false, false);
                    var parentNodes = getParentNodes_ztree(treeId, parentNode);
                    treeObj.expandNode(parentNodes, true, false, false);
                }
            }
        }
    }

    /**
     * 递归得到指定节点的父节点的父节点....直到根节点
     */
    function getParentNodes_ztree(treeId, node) {
        if (node != null) {
            var treeObj = $.fn.zTree.getZTreeObj(treeId);
            var parentNode = node.getParentNode();
            return getParentNodes_ztree(treeId, parentNode);
        } else {
            return node;
        }
    }

    
})(jQuery);
var setFontCss_ztree = function (treeId, treeNode) {
    return (!!treeNode.highlight) ? { border: "1px solid #428bca" } : { border: "none" };
}